package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;

import controlador.Controlador;
import controlador.ItemCtrl;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;
import javax.swing.border.BevelBorder;



import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Item extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();

	private Controlador controlador;
	private JTextField txtItem;
	private JTextField txtColor;
	private JTextField txtTalla;
	private JTextField txtLogo;
	private JTextField txtObs;
	private JTextField txtStockInicial;
	private JTextField txtStockMinimo;
	private ItemCtrl itemCtrl;
	private JLabel lblFoto;

	public Item() {
		setResizable(false);
		setTitle("Item");
		setIconImage(Toolkit
				.getDefaultToolkit()
				.getImage(
						Item.class
								.getResource("/modelo/imagenes/cute_pictures_2921cd4.jpg")));
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);



		JLabel lblItem = new JLabel("Item");
		lblItem.setBounds(10, 70, 46, 14);
		contentPanel.add(lblItem);

		JLabel lblColor = new JLabel("Color");
		lblColor.setBounds(10, 95, 46, 14);
		contentPanel.add(lblColor);

		JLabel lblTalla = new JLabel("Talla");
		lblTalla.setBounds(10, 120, 46, 14);
		contentPanel.add(lblTalla);

		JLabel lblLogo = new JLabel("Logo");
		lblLogo.setBounds(10, 145, 46, 14);
		contentPanel.add(lblLogo);

		JLabel lblImagen = new JLabel("Imagen");
		lblImagen.setBounds(226, 120, 46, 14);
		contentPanel.add(lblImagen);

		JLabel lblDescrpicinDelItem = new JLabel("Descripci\u00F3n del Item");
		lblDescrpicinDelItem.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblDescrpicinDelItem.setBounds(10, 24, 244, 25);
		contentPanel.add(lblDescrpicinDelItem);

		JLabel lblObservacin = new JLabel("Observaci\u00F3n");
		lblObservacin.setBounds(10, 170, 86, 14);
		contentPanel.add(lblObservacin);

		JLabel lblStockInicial = new JLabel("Stock inicial");
		lblStockInicial.setBounds(226, 70, 78, 14);
		contentPanel.add(lblStockInicial);

		JLabel lblStockMinimo = new JLabel("Stock minimo");
		lblStockMinimo.setBounds(226, 95, 78, 14);
		contentPanel.add(lblStockMinimo);

		txtItem = new JTextField();
		txtItem.setBounds(88, 67, 86, 20);
		contentPanel.add(txtItem);
		txtItem.setColumns(10);

		txtColor = new JTextField();
		txtColor.setBounds(88, 92, 86, 20);
		contentPanel.add(txtColor);
		txtColor.setColumns(10);

		txtTalla = new JTextField();
		txtTalla.setBounds(88, 117, 86, 20);
		contentPanel.add(txtTalla);
		txtTalla.setColumns(10);

		txtLogo = new JTextField();
		txtLogo.setBounds(88, 142, 86, 20);
		contentPanel.add(txtLogo);
		txtLogo.setColumns(10);

		txtObs = new JTextField();
		txtObs.setBounds(10, 198, 294, 20);
		contentPanel.add(txtObs);
		txtObs.setColumns(10);

		txtStockInicial = new JTextField();
		txtStockInicial.setBounds(314, 67, 86, 20);
		contentPanel.add(txtStockInicial);
		txtStockInicial.setColumns(10);

		txtStockMinimo = new JTextField();
		txtStockMinimo.setBounds(314, 92, 86, 20);
		contentPanel.add(txtStockMinimo);
		txtStockMinimo.setColumns(10);
		
		lblFoto = new JLabel("");
		lblFoto.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lblFoto.setBounds(314, 120, 86, 98);
		final int altoFoto = lblFoto.getHeight();
		final int anchoFoto = lblFoto.getWidth();
		lblFoto.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				itemCtrl = controlador.getItemCtrl();
				itemCtrl.setItem(controlador.getItem());
				itemCtrl.buscarImagen(altoFoto, anchoFoto);
									
				
			}
		});
		contentPanel.add(lblFoto);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						itemCtrl = controlador.getItemCtrl();
						itemCtrl.setTxtItem(txtItem.getText());
						itemCtrl.setTxtColor(txtColor.getText());
						itemCtrl.setTxtLogo(txtLogo.getText());
						itemCtrl.setTxtObs(txtObs.getText());
						itemCtrl.setTxtStockInicial(txtStockInicial.getText());
						itemCtrl.setTxtStockMinimo(txtStockMinimo.getText());
						itemCtrl.setTxtTalla(txtTalla.getText());

						if(itemCtrl.ingresarItem()){
							
							cerrarVentana();							
							controlador.getIngresarCtrl().actualizarComboBox();
							controlador.getIngresar().borrarCampos();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						controlador.getItemCtrl().limpiarFis();
						cerrarVentana();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	public void setControlador(Controlador miControlador) {
		this.controlador = miControlador;
	}
	
	public void cambiarImagen(Image icono){
		lblFoto.setIcon(new ImageIcon(icono));
	}

	private void cerrarVentana() {
		controlador.ocultarVentanaItem();
		
		txtColor.setText("");
		txtItem.setText("");
		txtLogo.setText("");
		txtObs.setText("");
		txtStockInicial.setText("");
		txtStockMinimo.setText("");
		txtTalla.setText("");
		lblFoto.setIcon(null);
	}
}
